package com.syyo.admin.utils;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * @Auther: wangzhong
 * @Date: 2019/10/16 09:43
 * @Description:
 */
public class MyStringUtils {

    /**
     * 将字符串转成列表  "0,2,4" => [0,2,4]
     * @param str
     * @return
     */
    public static List<Integer> stringToList(String str){
        List<Integer> list = new ArrayList<>();
        if (!StringUtils.isEmpty(str)){
            String[] splits = str.split(",");
            for (String split : splits) {
                list.add(Integer.parseInt(split));
            }
        }
        return list;
    }


    /**
     * 将整型列表转换为字符串 [0,2,4] => "0,2,4"
     * @param list
     * @return
     */
    public static String listToString(List<Integer> list){
        String pids =  "";
        if (list != null && list.size()>0){
            for (Integer integer : list) {
                pids = pids + ","+ integer;
            }
            return pids.substring(1, pids.length());
        }
        return "0";
    }

    public static boolean isNotEmpty(String str) {
        return str != null && !"".equals(str);
    }

    public static boolean isEmpty(String str) {
        return str == null || "".equals(str);
    }

    public static String getUUID(){
        return UUID.randomUUID().toString();
    }

    /**
     * 获取列表的最后一位数
     * @param list
     * @return
     */
    public static Integer getPid(List<Integer> list) {
        if (list != null && list.size()>0){
            return list.get(list.size()-1);
        }
        return 0;
    }

    /**
     * 将整型列表转换为字符串 [0,2,4] => "0,2,4"
     * @param list
     * @return
     */
    public static String getPids(List<Integer> list) {
        String pids =  "";
        if (list != null && list.size()>0){
            for (Integer integer : list) {
                pids = pids + ","+ integer;
            }
            return pids.substring(1, pids.length());
        }
        return "0";
    }
}
